#!/bin/bash
#
# http://www.maxwit.com
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


UTILS_TOP="${MW_TOP_DIR}/host"

BUILT_TAG=".maxwit_built"

BuildUtilPkg() # fixme for loop
{
	local df=""
	local subdir="${UTILS_TOP}/${1}"
	local srcdir="${UTILS_BUILD_PATH}/${1}"

	if [ -d "${subdir}" ]; then
		cd "${subdir}" 
	else
		echo "\"${subdir}\" does NOT exist!"
		return 1
	fi

	if [ -e "${srcdir}/${BUILT_TAG}" ]; then
		echo "\"${1}\" has been built."
		echo
		return 0
	fi

	if [ -f dep ]; then
		for df in `cat dep`
		do
			BuildUtilPkg ${df} || return 1
		done
	fi	

	CheckPkg ${1}

	Unpack ${1} host

	cd ${srcdir}

	${subdir}/build.sh ${UTILS_PREFIX} && \
	touch ${srcdir}/${BUILT_TAG}

	echo
	return 0
}


UtilsBuildList()
{
	local pkg=""

	for pkg in $*
	do
		BuildUtilPkg ${pkg} || exit 1
	done
}


BuildUtils()
{
	local UTILS_PKG_LIST="\
		${MWP_PKG_CONFIG} \
		${MWP_AUTOCONF} \
		${MWP_AUTOMAKE} \
		${MWP_PERL} \
		"

	local UTILS_PKG_LIST="\
		${MWP_PATCH} \
		${MWP_GETTEXT} \
		${MWP_ZLIB} \
		${MWP_M4} \
		${MWP_LIBTOOL} \
		${MWP_FLEX} \
		${MWP_BISON} \
		${MWP_SED} \
		${MWP_GAWK} \
		${MWP_TEXINFO} \
		${MWP_GMP} \
		${MWP_MPFR} \
		${MWP_GREP} \
		${MWP_SBOX2} \
		${MWP_QEMU} \
		"

	mkdir -vp ${UTILS_PREFIX}/bin
	mkdir -vp ${UTILS_PREFIX}/lib
	mkdir -vp ${UTILS_PREFIX}/share/man/man1
	echo

	UtilsBuildList ${UTILS_PKG_LIST}

#	sed -i "s:\(.*my.*ENV.*\)libtoolize:\1${UTILS_PREFIX}/bin/libtoolize:" ${UTILS_PREFIX}/bin/autoreconf || exit 1
}
